// pages/principaExam/principalExam.js
var app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
var answerList = [] //本地答案集合：id+value+answer+score
//实现单选、多选
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0, // 题目序列
        questionList: [],
        answerList: [],
        isShowAnswer: false //是否展示答案
    },
    mock: () => {
        // wx.showModal({
        //     content: '亲爱的各位同学：\n' + '很高兴使用金职伟业证书模拟考评系统。希望你们在证书考评中取得优秀成绩。正如教育部一直强调的，1+X证书是为了帮助大家掌握技能，提升就业能力。因此金职伟业积极对接企业，提供大批优质实习就业岗位。各位同学可以完善简历以获得更多实习就业机会。如果不希望推荐就业，也希望同学说出原因方便我们改进工作。感谢各位同学的支持和理解！\n' + '湖南金职伟业母婴护理有限公司',
        //     success(res) {
        //         if (res.confirm) {} else if (res.cancel) {
        //             wx.navigateBack()
        //         }
        //     },

        // })
    },
    getIndex: function (e) {
        var value = e.detail.value;
        this.setData({
            inputIndex: value
        });
    },
    //获取题目数据
    getPractice: function (grade) {
        var _this = this
        app._post_form(siteinfo.selTrainExaminationPaper, {
            grade: grade,
            profile_id: "1522773009475420162"
        }, function (result) {
            //计时
            util.calculateTime(_this, result.data.endTime)
            //  _this.setInterval(result.data.endTime)
            var practiceList = result.data.questions
            for (var i = 0; i < practiceList.length; i++) {
                practiceList[i].options = practiceList[i].options.split(";;")
                practiceList[i].checked = false
                for (var j = 0; j < practiceList[i].options.length; j++) {
                    practiceList[i].options[j] = {
                        "score": 0,
                        "code": practiceList[i].options[j].substring(0, 1),
                        "content": practiceList[i].options[j].substring(2, practiceList[i].options[j].length),
                        "checked": false
                    }
                }
            }
            _this.setData({
                questionList: practiceList, // 拿到答题数据
                tpaperId: result.data.id
            })
        })
    },
    /*
     * 单选事件
     */
    radioChange: function (e) {
        var _this = this
        var questionList = _this.data.questionList
        var index = _this.data.index
        for (var i = 0; i < questionList.length; i++) {
            if (questionList[index].id == questionList[i].id) {
                for (var j = 0; j < questionList[index].options.length; j++) {
                    //渲染答卷选项
                    if (e.detail.value == questionList[index].options[j].code) {
                        questionList[index].options[j].checked = true
                    } else {
                        questionList[index].options[j].checked = false
                    }
                }
                questionList[i].value = e.detail.value
            }
        }
        _this.setData({
            questionList: questionList
        })
    },
    /*
     * 多选事件
     */
    checkboxChange: function (e) {
        var _this = this
        var questionList = _this.data.questionList
        var index = _this.data.index
        for (var i = 0; i < questionList.length; i++) {
            if (questionList[index].id == questionList[i].id) {
                for (var j = 0; j < questionList[index].options.length; j++) {
                    questionList[index].options[j].checked = false
                    for (var b = 0; b < e.detail.value.length; b++) {
                        //渲染答卷选项
                        if (e.detail.value[b] == questionList[index].options[j].code) {
                            questionList[index].options[j].checked = true
                        }
                    }
                    questionList[i].value = e.detail.value
                }
            }
        }
        _this.setData({
            questionList: questionList
        })
    },
    goTo: function () {
        var _this = this
        var i = _this.data.inputIndex
        if (i >= 1 && i <= _this.data.questionList.length) {
            _this.setData({
                index: i - 1
            })
        } else {
            _this.setData({
                inputIndex: _this.data.index + 1
            })
            return;
        }
    },
    // 上一题
    prev: function () {
        var _this = this

        if (_this.data.index != 0) {
            // 渲染下一题
            _this.setData({
                index: _this.data.index - 1
            })
        }
    },
    // 提交答卷
    sumitPaper: function () {
        var _this = this
        //格式化答卷格式id+value
        answerList = []
        for (var i = 0; i < _this.data.questionList.length; i++) {
            if (typeof (_this.data.questionList[i].value) == 'object') {
                var bb = ''
                for (var j = 0; j < _this.data.questionList[i].value.length; j++) {
                    bb += _this.data.questionList[i].value[j] + ';;'
                }
                answerList.push({
                    id: _this.data.questionList[i].id,
                    value: bb,
                    answer: _this.data.questionList[i].answer,
                    score: _this.data.questionList[i].score
                });
            } else {
                answerList.push({
                    id: _this.data.questionList[i].id,
                    value: _this.data.questionList[i].value,
                    answer: _this.data.questionList[i].answer,
                    score: _this.data.questionList[i].score
                });
            }
        }
        // console.log("answerList", answerList)
        //计算总分数
        var allScore = 0;
        for (var i = 0; i <= answerList.length - 1; i++) {
            if (answerList[i].value != undefined) {
                //单选，判断
                if (answerList[i].value.toString().indexOf(";;") == -1) {
                    if (answerList[i].value == answerList[i].answer) {
                        allScore = allScore + answerList[i].score * 1
                    }
                } else {
                    //多选
                    var valueList = answerList[i].value.split(";;");
                    //数组重新排序
                    var valueList1 = valueList.slice(0, valueList.length - 1).sort()
                    var answerList1 = answerList[i].answer.split(";;").sort();
                    if (valueList1.length == answerList1.length) {
                        var b = true;
                        var c = '';
                        for (var a = 0; a < valueList1.length; a++) {
                            c += valueList1[a] + ";;"
                            if (a >= answerList1.length || valueList1[a] != answerList1[a]) {
                                b = false;
                            }
                        }
                        answerList[i].value = c.substring(0, c.length - 2);
                        if (b == true) {
                            allScore = allScore + answerList[i].score * 1
                        }
                    }
                }
            }
        }
        _this.setData({
            answerList: answerList
        })
        wx.showModal({
            title: '提示',
            content: '您的总分为' + allScore + '分',
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    answerList = []
                    _this.setData({
                        isShowAnswer: true
                    })
                }
            }
        })
    },
    /*
     * 下一题/提交 按钮
     */
    nextSubmit: function () {
        var _this = this
        _this.setData({
            prevBtn: true
        })
        // 判断是不是最后一题
        if (_this.data.index < _this.data.questionList.length - 1) {
            // 渲染下一题
            _this.setData({
                index: _this.data.index + 1
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '确定要提交答卷？',
                success: function (res) {
                    if (res.confirm) {
                        _this.sumitPaper()
                    }
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 只有一个试卷，所以写死
        this.getPractice("2")
        wx.setNavigationBarTitle({
            title: '长沙市托育机构负责人模拟考试',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.mock()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})